package top.ajun.mall.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ajun.common.utils.PageUtils;
import top.ajun.common.utils.Query;
import top.ajun.mall.product.dao.BrandDao;
import top.ajun.mall.product.dao.CategoryBrandRelationDao;
import top.ajun.mall.product.dao.CategoryDao;
import top.ajun.mall.product.entity.BrandEntity;
import top.ajun.mall.product.entity.CategoryBrandRelationEntity;
import top.ajun.mall.product.entity.CategoryEntity;
import top.ajun.mall.product.service.BrandService;
import top.ajun.mall.product.service.CategoryBrandRelationService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl
    extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity>
    implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
            new Query<CategoryBrandRelationEntity>().getPage(params),
            Wrappers.query()
        );
        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 查询品牌名称和分类名称并设置
        BrandEntity brandDetail = brandDao.selectById(brandId);
        CategoryEntity categoryDetail = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brandDetail.getName());
        categoryBrandRelation.setCatelogName(categoryDetail.getName());
        // 保存数据
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String brandName) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(brandName);
        this.update(
            relationEntity,
            Wrappers.<CategoryBrandRelationEntity>lambdaUpdate()
                .eq(CategoryBrandRelationEntity::getBrandId, brandId)
        );
    }

    @Override
    public void updateCategory(Long catId, String catelogName) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setCatelogId(catId);
        relationEntity.setCatelogName(catelogName);
        this.update(
            relationEntity,
            Wrappers.<CategoryBrandRelationEntity>lambdaUpdate()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId)
        );
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> ralationList = this.baseMapper.selectList(
            Wrappers.<CategoryBrandRelationEntity>lambdaQuery()
                .eq(CategoryBrandRelationEntity::getCatelogId, catId)
        );

        return ralationList.stream().map(item -> {
            Long brandId = item.getBrandId();
            return brandService.getById(brandId);
        }).collect(Collectors.toList());
    }

}